package handler

import (
	"food-manage/modelr"
	"food-manage/pkg"
	"github.com/gin-gonic/gin"
	"path/filepath"
	"strconv"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	cont := c.PostForm("cont")
	img := c.PostForm("img")
	enginID, _ := strconv.Atoi(c.PostForm("engin_id"))
	userID, _ := strconv.Atoi(c.PostForm("user_id"))
	engin := modelr.Engin{
		Title:    title,
		Cont:     cont,
		Img:      img,
		EnginNum: enginID,
		UserID:   userID,
	}
	err := modelr.DB.Debug().Create(&engin).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
	}
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("page", "3"))
	offset := (page - 1) * size

	var count int64
	modelr.DB.Model(&modelr.Engin{}).Count(&count)

	var user []modelr.Engin
	err := modelr.DB.Offset(offset).Limit(size).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "查询成功",
			"data": gin.H{
				"count": count,
				"user":  user,
			},
		})
	}
}
func Register(c *gin.Context) {
	name := c.PostForm("name")
	pass := c.PostForm("pass")
	if name == "" || pass == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号不能为空",
		})
		return
	}
	user := modelr.User{

		Name: name,
		Pass: pkg.Md(pass),
	}
	err := modelr.DB.Create(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "注册失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "注册成功",
		})
	}

}
func Login(c *gin.Context) {
	name := c.PostForm("name")
	pass := c.PostForm("pass")
	var user modelr.User
	err := modelr.DB.Where("name = ?", name).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账户不存在",
		})
		return
	}
	if pkg.Md(pass) != user.Pass {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})
}
func Upload(c *gin.Context) {
	file, err := c.FormFile("img")
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "文件获取失败",
		})
		return
	}
	if file.Size > 3*1024*1024 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "文件大小不能超过3M",
		})
		return
	}
	ext := filepath.Ext(file.Filename)
	if ext != ".png" && ext != ".jpg" && ext != ".jpeg" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "文件格式错误",
		})
		return
	}
	path := "static/img/" + file.Filename
	err = c.SaveUploadedFile(file, path)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "文件上传失败",
		})
		return
	} else {
		url := "http://" + c.Request.Host + "/" + path
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "上传成功",
			"data": url,
		})
	}
}
func Like(c *gin.Context) {
	userID, _ := strconv.Atoi(c.PostForm("user_id"))
	enginID, _ := strconv.Atoi(c.PostForm("engin_id"))
	var like modelr.Like
	err := modelr.DB.Where("user_id = ? and movie_id = ?", userID, enginID).Find(&like).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不可重复点赞",
		})
		return
	}
	like = modelr.Like{
		UserID: userID,

		EnginID: userID,
	}
	err = modelr.DB.Create(&like).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "点赞失败",
		})
		return
	}
	var engin modelr.Engin
	err = modelr.DB.Where("id=?", engin).Find(&engin).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "未找到视频",
		})
		return
	}
	engin.EnginNum++
	err = modelr.DB.Save(&like).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "更新失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "点赞成功",
	})
	return
}
